package com.rc.saas.mini.service.tenant;

import com.rc.saas.mini.mapper.car.TenantCarNumberMapper;
import com.rc.saas.mini.mapper.tenant.TenantMapper;
import com.rc.saas.mini.model.tenant.Tenant;
import com.rc.saas.mini.model.tenant.TenantExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author frog
 * @data 2019/9/21
 */
@Service
public class TenantServiceImpl implements TenantService {

    @Autowired
    private TenantMapper tenantMapper;

    @Autowired
    private TenantCarNumberMapper tenantCarNumberMapper;

    @Override
    public TenantMapper getMapper() {
        return tenantMapper;
    }

    @Override
    public Tenant selectByOriginalid(String miniOriginalid) {
        TenantExample example = new TenantExample();
        example.createCriteria().andMinipAppidEqualTo(miniOriginalid);
        List<Tenant> list = tenantMapper.selectByExample(example);
        if (list.size() != 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Tenant selectByAppid(String minipAppid) {
        TenantExample example = new TenantExample();
        example.createCriteria().andMinipAppidEqualTo(minipAppid);
        return tenantMapper.selectByExample(example).stream().findFirst().orElseGet(() -> null);
    }

    @Override
    public Tenant selectByTenantCode(String tenantCode) {
        TenantExample example = new TenantExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode);
        List<Tenant> list = tenantMapper.selectByExample(example);
        if (list.size() != 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Tenant selectTenantByMemberOpenid(String openId) {
        return tenantMapper.selectTenantByOpenid(openId);
    }

    @Override
    public Tenant selectTenantByMobile(String tenantCode, String mobile) {
        TenantExample example = new TenantExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andPhoneEqualTo(mobile);
        List<Tenant> list = tenantMapper.selectByExample(example);
        if (list.size() != 0) {
            return list.get(0);
        }
        return null;
    }

    //@Override
    //public PageInfo<Tenant> searchByCondition(BaseSearch search) {
    //    PageHelper.startPage(search.getOffset(), search.getLimit());
    //    List<Tenant> tenantList = tenantMapper.searchByCondition(search);
    //    return new PageInfo<Tenant>(tenantList);
    //}

    //@Override
    //public Tenant judgeLoginNameIsOnly(String phone) {
    //    TenantExample example = new TenantExample();
    //    example.createCriteria().andPhoneEqualTo(phone);
    //    List<Tenant> tenants = tenantMapper.selectByExample(example);
    //    if (tenants.size() != 0) {
    //        return tenants.get(0);
    //    }
    //    return null;
    //}
    //
    //@Override
    //public Tenant judgeTenantNameIsOnly(String tenantNazme) {
    //    TenantExample example = new TenantExample();
    //    example.createCriteria().andTenantCodeEqualTo(tenantNazme);
    //    List<Tenant> tenants = tenantMapper.selectByExample(example);
    //    if (tenants.size() != 0) {
    //        return tenants.get(0);
    //    }
    //    return null;
    //}



    @Override
    public List<Tenant> listByStatus(String status) {
        TenantExample example = new TenantExample();
        example.createCriteria().andStatusEqualTo(status);
        List<Tenant> list = tenantMapper.selectByExample(example);

        return list;
    }
}
